﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.WindowsMobile.Samples.Location;

namespace Mirkuz.TrailCompanion
{
    public partial class fGPSStat : Form
    {
        public fGPSStat()
        {
            InitializeComponent();
        }

        public void UpdateData(GpsPosition CurrentPosition)
        {

            //inizializzazione testi            
            lblLatitude.Text = "Lat: ";
            lblLongitude.Text = "Lon: ";
            lblAltitude.Text = "Alt: ";
            lblSNR.Text = "Sat: ";
            lblPDOP.Text = "PDOP: ";

            //se non ho posizione esco
            if (CurrentPosition == null)
            {
                lblFIX.Text = "No fix, no data";
                return;
            }

            //calcolo SNR
            float SolSnrAvg = 0;
            float ViewSnrAvg = 0;
            try
            {
                Satellite[] UsedSatellites = CurrentPosition.GetSatellitesInSolution();
                if (UsedSatellites != null && UsedSatellites.Length != 0)
                {
                    foreach (Satellite s in UsedSatellites)
                    {
                        SolSnrAvg += s.SignalStrength;
                    }
                    SolSnrAvg /= UsedSatellites.Length;
                }
            }
            catch { }
            try
            {
                Satellite[] ViewSatellites = CurrentPosition.GetSatellitesInView();
                if (ViewSatellites != null && ViewSatellites.Length != 0)
                {
                    foreach (Satellite s in ViewSatellites)
                    {
                        ViewSnrAvg += s.SignalStrength;
                    }
                    ViewSnrAvg /= ViewSatellites.Length;
                }
            }
            catch { }
            lblSNR.Text += "S:" + CurrentPosition.SatelliteCount.ToString() + " Snr:" + SolSnrAvg.ToString("N1") + "db, ";
            lblSNR.Text += "V:" + CurrentPosition.SatellitesInViewCount.ToString();

            lblPDOP.Text += CurrentPosition.PositionDilutionOfPrecision.ToString("N1");

            if (CurrentPosition.TimeValid == false || CurrentPosition.Time == null)
            {
                //posizione ok ma niente tempo
                lblFIX.Text = "No Time Fix";
                return;
            }

            lblFIX.Text = "Fix,";
            lblFIX.Text += CurrentPosition.Time.ToString("hh:mm:ss dd/MM/yyyy");

            if (SolSnrAvg < 15 
                && CurrentPosition.SatelliteCount < 4 
                || CurrentPosition.PositionDilutionOfPrecision > 8)
                return;


            lblLatitude.Text += CurrentPosition.Latitude.ToString("N10");
            lblLongitude.Text += CurrentPosition.Longitude.ToString("N10");
            lblAltitude.Text += CurrentPosition.SeaLevelAltitude.ToString("N0") + " mSLM";
        }

        private void mnuQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
